package com.dp.strategy.strategyFactory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: dupeng
 * @CreateTime: 2025-01-06  16:45
 * @Description: TODO
 */
@Component
public class StrategyFactory implements ApplicationContextAware {

    private static Map<String, Message> map = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, Message> beans = applicationContext.getBeansOfType(Message.class);
        beans.values().forEach(bean -> map.put(bean.getMessageType(), bean));
    }

    public static Message getStrategy(String name) {
        return map.get(name);
    }
}
